/**
 * 
 */
package fr.cephb.joperon.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import fr.cephb.joperon.core.Operon;
import fr.cephb.joperon.core.bio.Assembly;
import fr.cephb.joperon.tool.OperonFactory;
import fr.cephb.swing.ObjectAction;

/**
 * @author lindenb
 *
 */
public class OperonGUI extends JFrame
	{
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	private OperonFactory operonFactory;
	private JDesktopPane desktop;
	private JTextField statusBar;
	
	private OperonGUI(Operon operon)
		{
		super("Operon");
		this.operonFactory= new OperonFactory(operon);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter()
			{
			@Override
			public void windowOpened(WindowEvent e) {
				Dimension d= Toolkit.getDefaultToolkit().getScreenSize();
				OperonGUI.this.setBounds(100, 100, d.width-200, d.height-200);
				OperonGUI.this.validate();
				//JInternalFrame f= new SeqMapFrame(OperonGUI.this,getAssembly());
				//getDesktop().add(f);
				//f.setVisible(true);
				
				
				}
			@Override
			public void windowClosing(WindowEvent e)
				{
				doMenuQuit();
				}
			});
		
		JPanel content= new JPanel(new BorderLayout());
		setContentPane(content);
		content.add(this.desktop= new JDesktopPane(),BorderLayout.CENTER);
		this.statusBar= new JTextField("Idle",100);
		this.statusBar.setEditable(false);
		content.add(this.statusBar,BorderLayout.SOUTH);
		
		
		JMenuBar bar= new JMenuBar();
		setJMenuBar(bar);
		JMenu menu= new JMenu("File");
		bar.add(menu);
		menu.add(new AbstractAction("Quit")
				{
				private static final long serialVersionUID = 1L;
	
				@Override
				public void actionPerformed(ActionEvent e) {
					doMenuQuit();
					}
				}
			);
		menu= new JMenu("Assembly");
		bar.add(menu);
		
		
		ButtonGroup buttonGroup=new ButtonGroup();
		
		
		for(Assembly ass: Assembly.getAssemblies())
			{
			JCheckBoxMenuItem item= new JCheckBoxMenuItem(new ObjectAction<Assembly>(ass)
					{
					private static final long serialVersionUID = 1L;
					@Override
					public void actionPerformed(ActionEvent e)
						{
						getOperonFactory().setAssembly( getObject());
						}
					}
				);
			item.setSelected(ass==Assembly.getDefaultAssembly());
			menu.add(item);
			buttonGroup.add(item);
			}
		getOperonFactory().setAssembly( Assembly.getDefaultAssembly());
		
		menu= new JMenu("Database");
		bar.add(menu);
		AbstractAction action=new AbstractAction("Projects")
			{
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame f= new PrjMgrFrame(OperonGUI.this);
				getDesktop().add(f);
				f.setVisible(true);
				}
			};
		menu.add(action);
		
		}
	
	private void doMenuQuit()
		{
		this.setVisible(false);
		this.dispose();
		
		Operon op= getOperonFactory().getOperon();
		try{
			getOperonFactory().close();
			} catch(Throwable err) { err.printStackTrace();}
		try{
		op.close();
		} catch(Throwable err) { err.printStackTrace();}
		}
		
	
	public Assembly getAssembly() {
		return getOperonFactory().getAssembly();
		}
	
	public OperonFactory getOperonFactory() {
		return operonFactory;
		}
	
	
	public Operon getOperon()
		{
		return getOperonFactory().getOperon();
		}
	
	public JDesktopPane getDesktop() {
		return desktop;
		}
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
		{
		try {
			JFrame.setDefaultLookAndFeelDecorated(true);
			JDialog.setDefaultLookAndFeelDecorated(true);
			Operon operon= Operon.newInstance();
			final OperonGUI op= new OperonGUI(operon);
			SwingUtilities.invokeAndWait(new Runnable()
				{
				@Override
				public void run() {
					op.setVisible(true);
					}
				});
			}
		catch (Exception e)
			{
			e.printStackTrace();
			}
	}

}
